package com.inf.service;

import com.inf.dao.PersonRepoDao;
import com.inf.featureCompare.FeatureCompare;
import com.inf.utils.Tools;
import com.inf.vo.ResultPartnerVo;
import com.inf.vo.ParamsPartnerVo;
import com.sun.jersey.core.util.Base64;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;

/**
 * Created by zhangbn on 2018/11/8.
 */
@Service
public class PartnerAnalysisService {

    Logger log = LoggerFactory.getLogger(PartnerAnalysisService.class);

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private PersonRepoDao personRepoDao;

    public List<ResultPartnerVo> getPartner(ParamsPartnerVo vo) {
        List<ResultPartnerVo> voList = new ArrayList<>();
        try {
            Jedis jedis = jedisPool.getResource();
            List<byte[]> listN = Tools.getPersonRepoFeatureList();

            byte[] var1 = Base64.decode(vo.getFeature());
            double[] dArr = new FeatureCompare().featureCompByByte(var1, listN);

            Map<Integer, Double> rst = Tools.getMaxScoreAndIdx(dArr);
            String rowkey = "";
            Iterator<Map.Entry<Integer, Double>> it = rst.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Integer, Double> next = it.next();
                if (next.getValue().compareTo(Double.valueOf(0.7)) > 0) {
                    rowkey = jedis.lindex("rowkeyList", next.getKey());
                    log.info("show: " + "找到要搜索的图片的idx= " + next.getKey() + "    相似度=" + next.getValue());
                    log.info("show: " + "找到要搜索的图片的uuid= " + rowkey);
                } else {
                    log.info("show: " + "没有找到要搜索的图片！");
                }
            }
            if (!StringUtils.isEmpty(rowkey)) {
                voList = personRepoDao.queryPartnerByRangeAndRowkeyInOriginal("bigdata:person", vo, rowkey);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return voList;
    }


    public List<ResultPartnerVo> getPartnerView(ParamsPartnerVo vo) {
        List<ResultPartnerVo> voList = new ArrayList<>();
        try {
            Jedis jedis = jedisPool.getResource();
            List<byte[]> listN = Tools.getPersonRepoFeatureList();

            byte[] var1 = Base64.decode(vo.getFeature());
            double[] dArr = new FeatureCompare().featureCompByByte(var1, listN);

            Map<Integer, Double> rst = Tools.getMaxScoreAndIdx(dArr);
            String rowkey = "";
            Iterator<Map.Entry<Integer, Double>> it = rst.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Integer, Double> next = it.next();
                if (next.getValue().compareTo(Double.valueOf(0.7)) > 0) {
                    rowkey = jedis.lindex("rowkeyList", next.getKey());
                    log.info("show: " + "找到要搜索的图片的idx= " + next.getKey() + "    相似度=" + next.getValue());
                    log.info("show: " + "找到要搜索的图片的uuid= " + rowkey);
                } else {
                    log.info("show: " + "没有找到要搜索的图片！");
                }
            }
            if (!StringUtils.isEmpty(rowkey)) {
                voList = personRepoDao.queryPartnerByRangeAndRowkeyInOriginalView("bigdata:person", vo, rowkey);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return voList;
    }
}
